//
//  LaunchActivityViewController.m
//  wtshoppingcite
//
//  Created by Baojia-LC on 14/11/10.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "LaunchActivityViewController.h"
#import "PostPhotoView.h"
#import "CustomBlueTextView.h"
#import "TimeSecondPickView.h"
@interface LaunchActivityViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property(nonatomic,strong)NSArray* titles;
@property(nonatomic,strong)NSMutableDictionary* contentData;
@property(nonatomic,strong)UITableView* infoTableView;

@end

@implementation LaunchActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleView:@"发起活动"];
    [self setRightButton:@"提交" sel:@selector(commit:) viewController:self];
    
    _titles = @[@"主题",@"地点",@"联系人手机",@"开始时间",@"结束时间",@"报名截止时间",@"限制活动人数"];
    _contentData = [NSMutableDictionary dictionaryWithCapacity:0];
    for(NSString* key in _titles){
        [_contentData setObject:@"" forKey:key];
    }
    
    _infoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _infoTableView.delegate = self;
    _infoTableView.dataSource = self;
    _infoTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_infoTableView];
    
    UIView* backview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240)];
    UIImageView* line = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, kWidth-10,1)];
    line.backgroundColor = COLOR_RGB(227, 227, 227);
    [backview addSubview:line];
    
    PostPhotoView* postPhotoView = [[PostPhotoView alloc] initWithFrame:CGRectMake(5, 10, kWidth-10, 90)];
    [backview addSubview:postPhotoView];
    
    CustomBlueTextView* infoTextView = [[CustomBlueTextView alloc] initWithFrame:CGRectMake(postPhotoView.left, postPhotoView.bottom +10, kWidth-10, 120)withPlaceHolder:@"活动详情:"];
    [infoTextView setTextViewBorderColor:COLOR_RGB(235, 235, 235)];
    [backview addSubview:infoTextView];
    [_infoTableView setTableFooterView:backview];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titles.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indentify = @"cee";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indentify];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentify];
        
        UILabel* label = [self createLabelWithFrame:CGRectMake(20, 10, 100, 20) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_GRAY_COLOR];
        label.tag = 101;
        [cell.contentView addSubview:label];
        
        if(indexPath.row != 3 && indexPath.row != 4 && indexPath.row != 5){
            UITextField* textFeild = [self createTextFieldWithFrame:CGRectMake(label.right, label.top, 180, 20) text:@"" fontSize:14];
            textFeild.tag = indexPath.row+1111;
            textFeild.returnKeyType = UIReturnKeyDone;
            textFeild.delegate = self;
            [cell.contentView addSubview:textFeild];
        }
    }
    
    UILabel* label = (UILabel*)[cell.contentView viewWithTag:101];
    label.text = _titles[indexPath.row];
    //    UITextField* textFiled = (UITextField*)[cell.contentView viewWithTag:102];
    //    textFiled.tag = indexPath.row+1111;
    
    if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5)
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 3:
        {
            TimeSecondPickView* timePickerView = [[TimeSecondPickView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
            timePickerView.titleLabel.text = @"期望上门时间段";
            [timePickerView showView:self.view];
            [self.view addSubview:timePickerView];
        }break;
        case 4:{
            
            TimeSecondPickView* timePickerView = [[TimeSecondPickView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
            timePickerView.titleLabel.text = @"期望结束时间段";
            [timePickerView showView:self.view];
            [self.view addSubview:timePickerView];
            
        }break;
        case 5:{
            
        }break;
        default:
            break;
    }
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    int row = textField.tag - 1111;
    
    //    NSIndexPath* indexpath;
    //    if(row == 2){
    //        indexpath = [NSIndexPath indexPathForRow:row+4 inSection:0];
    //
    //    }else{
    //        indexpath = [NSIndexPath indexPathForRow:row+1 inSection:0];
    //    }
    NSIndexPath* indexpath = [NSIndexPath indexPathForRow:row inSection:0];
    [_infoTableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
    //    UITableViewCell* cell = (UITableViewCell*)[_infoTableView cellForRowAtIndexPath:indexpath];
    //    UITextField* nexttextFeild = (UITextField*)[cell.contentView viewWithTag:indexpath.row+1111];
    //    [nexttextFeild becomeFirstResponder];
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField;{
    
    int row = textField.tag - 1111;
    NSString* key = _titles[row];
    [_contentData setObject:textField.text forKey:key];
    
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
    
}

-(void)commit:(UIButton*)button{
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
